package com.ukee.birthday.modules.service.impl;

import com.ukee.birthday.common.utils.Constant;
import com.ukee.birthday.modules.dao.StaffDao;
import com.ukee.birthday.modules.dao.SysMenuDao;
import com.ukee.birthday.modules.dao.SysUserDao;
import com.ukee.birthday.modules.dao.SysUserTokenDao;
import com.ukee.birthday.modules.entity.StaffEntity;
import com.ukee.birthday.modules.entity.SysMenuEntity;
import com.ukee.birthday.modules.entity.SysUserEntity;
import com.ukee.birthday.modules.entity.SysUserTokenEntity;
import com.ukee.birthday.modules.service.ShiroService;
import com.ukee.birthday.modules.service.StaffService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ShiroServiceImpl implements ShiroService {

    @Resource
    private SysMenuDao sysMenuDao;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysUserTokenDao sysUserTokenDao;
    @Resource
    private StaffService staffService;

    @Override
    public Set<String> getUserPermissions(long userId) {
        List<String> permsList;

        //系统管理员，拥有最高权限
        if(userId == Constant.SUPER_ADMIN){
            List<SysMenuEntity> menuList = sysMenuDao.selectList(null);
            permsList = new ArrayList<>(menuList.size());
            for(SysMenuEntity menu : menuList){
                permsList.add(menu.getPerms());
            }
        }else {
//            permsList = sysUserDao.queryAllPerms(userId);
        }

        //用户权限列表
        Set<String> permsSet = new HashSet<>();
//        for(String perms : permsList) {
//            if (StringUtils.isBlank(perms)) {
//                continue;
//            }
//            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
//        }
        return permsSet;
    }

    @Override
    public SysUserTokenEntity queryByToken(String token) {
        return sysUserTokenDao.queryByToken(token);
    }

    @Override
    public SysUserEntity queryUser(Long userId) {
        return sysUserDao.selectById(userId);
    }

    @Override
    public StaffEntity queryStaff(Long userId) {
        return staffService.getById(userId);
    }
}
